package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdUserMapper;
import com.heima.admin.service.UserLoginService;
import com.heima.model.admin.dtos.AdUserDto;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppJwtUtil;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 胡慧
 * @title: UserLoginServiceImpl
 * @projectName heima-leadnews
 * @description: TODO
 * @date 2021/2/518:05
 */
@Service
@Transactional
@Slf4j
public class UserLoginServiceImpl extends ServiceImpl<AdUserMapper,AdUser> implements UserLoginService {
    @Override
    public ResponseResult login(AdUserDto dto) {
        //判断参数
        if(StringUtils.isBlank(dto.getName()) || StringUtils.isBlank(dto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户名或密码不能为空");
        }
        LambdaQueryWrapper<AdUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq( AdUser::getName,dto.getName());
        //根据名称查询参数
        AdUser adUser = getOne(wrapper);
        if (adUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"用户不存在");
        }
            String salt = adUser.getSalt();
            String password = dto.getPassword();
            //    生成密码
            String pswd = DigestUtils.md5DigestAsHex((password + salt).getBytes());

            if (pswd.equals(adUser.getPassword())){
                //生成jwt并返回
               Map map = new HashMap<>();
               map.put("token",AppJwtUtil.getToken(Long.valueOf(adUser.getId())));
               //数据不应该返回前台
                adUser.setPassword("");
                adUser.setSalt("");
               map.put("user",adUser );
                return ResponseResult.okResult(map);
            }else {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }



    }

    @Override
    public ResponseResult register(AdUser adUser) {
        //自己写一个password，然后转成加密格式
        //1.判断是否为空
        if (adUser==null|| adUser.getName()==null|| adUser.getPassword()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //根据条件查询
        LambdaQueryWrapper<AdUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdUser::getName,adUser.getName());
        AdUser one = getOne(wrapper);

        if (one!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户名重复，请更换");
        }
        String salt = RandomStringUtils.randomAlphanumeric(10);
        String password = adUser.getPassword();
        String pswd = DigestUtils.md5DigestAsHex((password + salt).getBytes());
        adUser.setPassword(pswd);
        adUser.setLoginTime(new Date());
        adUser.setCreatedTime(new Date());
        //默认0，表示暂时不可用
        adUser.setStatus(0);
        save(adUser);
        return ResponseResult.okResult("用户注册成功");
    }

}
